package ai.people.platform.archives.controller;

import ai.people.netmon.framework.domain.archives.vo.ArchivesEdgesVO;
import ai.people.netmon.framework.domain.graph.FrontGraphBase;
import ai.people.netmon.framework.domain.archives.vo.ArchivesGraph;
import ai.people.netmon.framework.domain.archives.vo.ArchivesTagsVO;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.archives.service.GraphService;
import ai.people.platform.archives.service.PersonArchivesEdgesService;
import ai.people.platform.archives.service.PersonArchivesTagsService;
import com.vesoft.nebula.client.graph.exception.IOErrorException;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 档案v2图接口
 *
 * @author zhenglin
 * @date 2022/4/8
 */
@RefreshScope
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/v2/graph")
public class GraphController {

    /**
     * 图服务
     */
    private final GraphService graphServiceImpl;
    /**
     * 人员档案标签服务impl
     */
    private final PersonArchivesTagsService personArchivesTagsServiceImpl;
    /**
     * 人员档案边服务impl
     */
    private final PersonArchivesEdgesService personArchivesEdgesServiceImpl;

    /**
     * 探索档案主体(展开档案主体图)
     *
     * @param id 档案ID
     * @return {@link Result}<{@link ArchivesGraph}>
     * @throws Exception 异常
     */
    @GetMapping("/path/{id}")
    public Result<ArchivesGraph> path(@PathVariable("id") Long id) throws Exception {
        ArchivesGraph path = graphServiceImpl.archivesPath(id, false);
        return ResultUtil.success(path);
    }

    /**
     * 探索档案主体(展开档案主体图) 隐藏信息
     *
     * @param id id
     * @return {@link Result}<{@link ArchivesGraph}>
     * @throws Exception 异常
     */
    @GetMapping("/path/hide/{id}")
    public Result<ArchivesGraph> hide(@PathVariable("id") Long id) throws Exception {
        ArchivesGraph path = graphServiceImpl.archivesPath(id,true);
        return ResultUtil.success(path);
    }


    /**
     * 检查图(只检查孤点)
     *
     * @param archivesGraphBaseVO 档案图
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/checkGraph")
    public Result<Boolean> checkGraph(@Validated @RequestBody FrontGraphBase archivesGraphBaseVO) {
        return ResultUtil.success(graphServiceImpl.checkGraph(archivesGraphBaseVO));
    }


    /**
     * 查询节点的属性详情
     *
     * @param vid vid
     * @return {@link Result}<{@link ArchivesGraph.Node}>
     * @throws IOErrorException             异常信息
     * @throws UnsupportedEncodingException 不支持编码异常
     */
    @GetMapping("/getNode/{vid}")
    public Result<ArchivesGraph.Node> getNode(@PathVariable("vid") String vid) throws Exception {
        ArchivesGraph.Node node = graphServiceImpl.getNodeVo(vid);
        return ResultUtil.success(node);
    }

    /**
     * 图tags(节点类型)列表
     *
     * @return {@link Result}<{@link List}<{@link ArchivesTagsVO}>>
     */
    @GetMapping("/listTags")
    public Result<List<ArchivesTagsVO>> listTags() {
        List<ArchivesTagsVO> tags = personArchivesTagsServiceImpl.listTags();
        return ResultUtil.success(tags);
    }

    /**
     * 图edges(边、关系)列表
     *
     * @return {@link Result}<{@link List}<{@link ArchivesEdgesVO}>>
     */
    @GetMapping("/listEdges")
    public Result<List<ArchivesEdgesVO>> listEdges() {
        List<ArchivesEdgesVO> edges = personArchivesEdgesServiceImpl.listEdges();
        return ResultUtil.success(edges);
    }

    /**
     * 档案主题字段和基本信息关系映射
     *
     * @return {@link Result}<{@link Map}<{@link String}, {@link String}>>
     */
    @GetMapping("/getEdgeMapping")
    public Result<Map<String, String>> getEdgeMapping() {
        Map<String, String> map = graphServiceImpl.getEdgeMapping();
        return ResultUtil.success(map);
    }
}

